#region File Description
//-----------------------------------------------------------------------------
// InputState.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace PokeTD {
    /// <summary>
    /// Helper for reading input from keyboard, gamepad, and touch input. This class 
    /// tracks both the current and previous state of the input devices, and implements 
    /// query methods for high level input actions such as "move up through the menu"
    /// or "pause the game".
    /// </summary>
    public class InputState {
        private KeyboardState _currentKeyboardState;
        private GamePadState _currentGamePadState;
        private MouseState _currentMouseState;

        private KeyboardState _lastKeyboardState;
        private GamePadState _lastGamePadState;
        private MouseState _lastMouseState;

        private bool _gamePadWasConnected;


        public KeyboardState CurrentKeyboardState {
            get { return _currentKeyboardState; }
        }
        public GamePadState CurrentGamePadState {
            get { return _currentGamePadState; }
        }
        public MouseState CurrentMouseState {
            get { return _currentMouseState; }
        }
       
        public KeyboardState LastKeyboardState {
            get { return _lastKeyboardState; }
        }
        public GamePadState LastGamePadState {
            get { return _lastGamePadState; }
        }
        public MouseState LastMouseState {
            get { return _lastMouseState; }
        }       

        public bool GamePadWasConnected {
            get { return _gamePadWasConnected; }
        }
        

        /// <summary>
        /// Constructs a new input state.
        /// </summary>
        public InputState() {
            _currentKeyboardState = new KeyboardState();
            _currentGamePadState = new GamePadState();
            _currentMouseState = new MouseState();

            _lastKeyboardState = new KeyboardState();
            _lastGamePadState = new GamePadState();
            _lastMouseState = new MouseState();

            _gamePadWasConnected = false;
        }

        /// <summary>
        /// Reads the latest state user input.
        /// </summary>
        public void Update() {
            _lastKeyboardState = CurrentKeyboardState;
            _lastGamePadState = CurrentGamePadState;
            _lastMouseState = CurrentMouseState;

            _currentKeyboardState = Keyboard.GetState();
            _currentGamePadState = GamePad.GetState(PlayerIndex.One);
            _currentMouseState = Mouse.GetState();

            // Keep track of whether a gamepad has ever been
            // connected, so we can detect if it is unplugged.
            if (CurrentGamePadState.IsConnected) {
                _gamePadWasConnected = true;
            }

            // Get the raw touch state from the TouchPanel
            //TouchState = TouchPanel.GetState();

            // Read in any detected gestures into our list for the screens to later process
            //Gestures.Clear();
            /*while (TouchPanel.IsGestureAvailable)
            {
                Gestures.Add(TouchPanel.ReadGesture());
            }*/
        }


        /// <summary>
        /// Helper for checking if a key was pressed during this update. 
        /// </summary>
        public bool IsKeyPressed(Keys key) {
            return CurrentKeyboardState.IsKeyDown(key);
            
        }


        /// <summary>
        /// Helper for checking if a button was pressed during this update.
        /// </summary>
        public bool IsButtonPressed(Buttons button) {
            return CurrentGamePadState.IsButtonDown(button);
        }

        /// <summary>
        /// Helper for checking if a key was newly pressed during this update.
        /// </summary>
        public bool IsNewKeyPress(Keys key) {
            return (CurrentKeyboardState.IsKeyDown(key) &&
                    LastKeyboardState.IsKeyUp(key));
        }


        /// <summary>
        /// Helper for checking if a button was newly pressed during this update.
        /// </summary>
        public bool IsNewButtonPress(Buttons button) {
            return (CurrentGamePadState.IsButtonDown(button) &&
                    LastGamePadState.IsButtonUp(button));
        }

        public bool IsLeftClick() {
            return LastMouseState.LeftButton == ButtonState.Released && CurrentMouseState.LeftButton == ButtonState.Pressed;
        }

        public bool IsRightClick() {
            return LastMouseState.RightButton == ButtonState.Released && CurrentMouseState.RightButton == ButtonState.Pressed;
        }

        public bool IsMiddleClick() {
            return LastMouseState.MiddleButton == ButtonState.Released && CurrentMouseState.MiddleButton == ButtonState.Pressed;
        }


    }
}
